const baseApiUrl = "https://chejian.wxqiandao.com/api" //正式
export const uploadFile = (event, filelist, apiUrl='/index/base/file/upload', cb) => {
	// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
	let lists = [].concat(event.file)
	let fileListLen = filelist.length
	lists.map((item) => {
		filelist.push({
			...item,
			status: 'uploading',
			message: '上传中'
		})
	})
	for (let i = 0; i < lists.length; i++) {
		// const result =
		uploadFilePromise(lists[i].url, apiUrl).then(result=>{
			let item = filelist[fileListLen]
			filelist.splice(fileListLen, 1, Object.assign(item, {
				status: 'success',
				message: '',
				url: result
			}))
			fileListLen++
			cb(filelist)
		})
	}
}

const uploadFilePromise = (url, apiUrl) => {
	return new Promise((resolve, reject) => {
		let a = uni.uploadFile({
			//url: this.$common.domain+'/api/common/upload', // 仅为示例，非真实的接口地址
			url: baseApiUrl + apiUrl, // 仅为示例，非真实的接口地址
			filePath: url,
			name: 'file',
			header: {
				Token: uni.getStorageSync('token')
			},
			formData: {
				user: 'test'
			},
			success: (res) => {
				resolve(JSON.parse(res.data).data.fullurl)
			}
		});
	})
}

export const uploadFileSimple = (event, apiUrl='/index/base/file/upload', cb) => {
	// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
	let item = event.file
	uploadFilePromise(item.url, apiUrl).then(result=>{
		cb(result)
	})
}